var gatetotal;//异常总页数
var sensortotal;//异常总页数

function init() {
    $.ajax({
        type:"get",
        url:"http://iot.wduozhi.xyz/api/gatewayException/page/" + 1,
        dataType:"json",
        success:function (data) {//请求成功后处理函数
            gatetotal = data.data.totalPages
            $('#gateway_page').text(0 + "/" + gatetotal);
        }
    }),

    $.ajax({
        type:"get",
        url:"http://iot.wduozhi.xyz/api/sensorException/page/" + 1,
        dataType:"json",
        success:function (data) {//请求成功后处理函数
            sensortotal = data.data.totalPages
            $('#sensor_page').text(0 + "/" + sensortotal)
        }
    })
}

function gate_excep() {//显示网关异常的函数
    var page = $("#gatepage").val();//网关异常页数
    if (page > gatetotal || page < 0){
        alert("输入页数超出正确范围！");
        $("#sensorpage").val("");   //将输入框清空
        return
    }
    var gate = "<caption style=\"text-align: center\">网关实时异常表</caption>" +
        "<tr style=\"height: 18pt;\">\n" +
        "<th style=\"width: 150pt; text-align: center;\">网关ID</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常时间</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常描述</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常地点</th>\n" +
        "</tr>";
    $.ajax({
        type:"get",
        url:"http://iot.wduozhi.xyz/api/gatewayException/page/" + page,
        dataType:"json",
        success:function (data) {//请求成功后处理函数
            $("#exception").empty();
            $("#exception").append(gate);
            console.log(data);
            $("#gatepage").val("");   //将输入框清空
            $("#myModal").click();  //关闭输入网关异常页数的窗口
            $('#gateway_page').text(page + "/" + gatetotal);
            for (var index in data.data.content) {
                var temp = data.data.content[index];
                var gate_id = temp.gateway.id;  //出现异常的网关ID
                var time = temp.time;   //异常出现的时间
                var description = temp.description;   //异常描述
                var location = temp.gateway.location;  //异常地点
                var html = "<tr>";
                html += "<td>" + gate_id + "</td>";
                html += "<td>" + time + "</td>";
                html += "<td>" + description + "</td>";
                html += "<td>" + location + "</td>";
                $("#exception").append(html);
            }
        }
    })
}


function sensor_excep() {//显示传感器异常的函数
    var page = $("#sensorpage").val();//传感器异常页数
    if (page > sensortotal){
        alert("页数超过总页数！");
        $("#sensorpage").val("");   //将输入框清空
        return
    }
    var sensor = "<caption style=\"text-align: center\">传感器实时异常表</caption>" +
        "<tr style=\"height: 18pt;\">\n" +
        "<th style=\"width: 150pt; text-align: center;\">传感器ID</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常时间</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常描述</th>\n" +
        "<th style=\"width: 150pt; text-align: center;\">异常地点</th>\n" +
        "</tr>";
    $.ajax({
        type:"get",
        url:"http://iot.wduozhi.xyz/api/sensorException/page/" + page,
        dataType:"json",
        data:{
            page:page
        },
        success:function (data) {//请求成功后处理函数
            $("#exception").empty();
            $("#exception").append(sensor);
            console.log(data);
            $("#sensorpage").val("");   //将输入框清空
            $("#iModal").click();  //关闭输入网关异常页数的窗口


            $('#sensor_page').text(page + "/" + sensortotal)
            for (var index in data.data.content) {
                var temp = data.data.content[index];
                var gate_id = temp.sensor.id;  //出现异常的传感器ID
                var time = temp.time;   //异常出现的时间
                var description = temp.description;   //异常描述
                var location = temp.sensor.location;  //异常地点
                var html = "<tr>";
                html += "<td>" + gate_id + "</td>";
                html += "<td>" + time + "</td>";
                html += "<td>" + description + "</td>";
                html += "<td>" + location + "</td>";
                $("#exception").append(html);
            }
        }
    })
}

//下载所有的网关异常
function downloadgate(){
    var a = window.confirm("确定下载网关异常？")
    if(a === true){
        var url = "http://iot.wduozhi.xyz/api/download/GatewayException"
        window.location.href = url
    }
}
//下载所有的传感器异常
function downloadsensor(){
    var a = window.confirm("确定下载传感器异常？")
    if(a === true){
        var url = "http://iot.wduozhi.xyz/api/download/SensorException"
        window.location.href = url
    }
}